<?php




abstract class Zend_Search_Lucene_Search_Query
{
    
    private $_boost = 1;

    
    protected $_weight = null;

    
    private $_currentColorIndex = 0;

    
    public function getBoost()
    {
        return $this->_boost;
    }

    
    public function setBoost($boost)
    {
        $this->_boost = $boost;
    }

    
    abstract public function score($docId, Zend_Search_Lucene_Interface $reader);

    
    abstract public function matchedDocs();

    
    abstract public function execute(Zend_Search_Lucene_Interface $reader, $docsFilter = null);

    
    abstract public function createWeight(Zend_Search_Lucene_Interface $reader);

    
    protected function _initWeight(Zend_Search_Lucene_Interface $reader)
    {
        // Check, that it's a top-level query and query weight is not initialized yet.
        if ($this->_weight !== null) {
            return $this->_weight;
        }

        $this->createWeight($reader);
        $sum = $this->_weight->sumOfSquaredWeights();
        $queryNorm = $reader->getSimilarity()->queryNorm($sum);
        $this->_weight->normalize($queryNorm);
    }

    
    abstract public function rewrite(Zend_Search_Lucene_Interface $index);

    
    abstract public function optimize(Zend_Search_Lucene_Interface $index);

    
    public function reset()
    {
        $this->_weight = null;
    }


    
    abstract public function __toString();

    
    abstract public function getQueryTerms();

    
    abstract protected function _highlightMatches(Zend_Search_Lucene_Search_Highlighter_Interface $highlighter);

    
    public function highlightMatches($inputHTML, $defaultEncoding = '', $highlighter = null)
    {
        if ($highlighter === null) {
            require_once 'Zend/Search/Lucene/Search/Highlighter/Default.php';
            $highlighter = new Zend_Search_Lucene_Search_Highlighter_Default();
        }

        
        require_once 'Zend/Search/Lucene/Document/Html.php';

        $doc = Zend_Search_Lucene_Document_Html::loadHTML($inputHTML, false, $defaultEncoding);
        $highlighter->setDocument($doc);

        $this->_highlightMatches($highlighter);

        return $doc->getHTML();
    }

    
    public function htmlFragmentHighlightMatches($inputHtmlFragment, $encoding = 'UTF-8', $highlighter = null)
    {
        if ($highlighter === null) {
            require_once 'Zend/Search/Lucene/Search/Highlighter/Default.php';
            $highlighter = new Zend_Search_Lucene_Search_Highlighter_Default();
        }

        $inputHTML = '<html><head><META HTTP-EQUIV="Content-type" CONTENT="text/html; charset=UTF-8"/></head><body>'
                   . iconv($encoding, 'UTF-8//IGNORE', $inputHtmlFragment) . '</body></html>';

        
        require_once 'Zend/Search/Lucene/Document/Html.php';

        $doc = Zend_Search_Lucene_Document_Html::loadHTML($inputHTML);
        $highlighter->setDocument($doc);

        $this->_highlightMatches($highlighter);

        return $doc->getHtmlBody();
    }
}

